package br.ufrn.dimap.MetricDslLanguage.validation;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.validation.Check;

import br.ufrn.dimap.MetricDslLanguage.metricDsl.ActivityMetric;
import br.ufrn.dimap.MetricDslLanguage.metricDsl.ArtefactMetric;
import br.ufrn.dimap.MetricDslLanguage.metricDsl.MetricDslPackage;
import br.ufrn.dimap.MetricDslLanguage.metricDsl.Metrics;
import br.ufrn.dimap.MetricDslLanguage.metricDsl.TaskMetric;
import br.ufrn.dimap.processDsl.Activity;
import br.ufrn.dimap.processDsl.Artifact;
import br.ufrn.dimap.processDsl.Task;

public class MetricDslJavaValidator extends AbstractMetricDslJavaValidator {
	/*
	@Check
	public void checkTaskIsValid(Metrics metrics) {
		boolean isValid = false;
		if (metrics.getDetails() instanceof TaskMetric)		
		for (Task _task : metrics.getRelatesTo().getTasks() )	
			for (Task task : ((TaskMetric)metrics.getDetails()).getTasks())					
				if (task.equals(_task.getName()))
					isValid = true;
		if (!isValid) 
			error("Invalid task reference", MetricDslPackage.Literals.METRICS__DETAILS);
	}
	*/

	
	@Check
	public void checkReferenceIsValid(Metrics metrics) {
		boolean existe = false;		
		if (metrics.getDetails() instanceof ArtefactMetric) {		
			for (Artifact _artifact : metrics.getRelatesTo().getArtifacts()) {	
				for (Artifact artifact : ((ArtefactMetric)metrics.getDetails()).getArtefacts())					
					if (artifact.getName().equals(_artifact.getName())) {						
						existe = true;
						break;
					}				
			}
			if (!existe) error("Invalid artifact reference. Artifact exists in different process related", MetricDslPackage.Literals.METRICS__DETAILS);
		}
		else if (metrics.getDetails() instanceof ActivityMetric) {
				for (EObject _activity : metrics.getRelatesTo().getLifecyleElement()) {
					//if (_activity instanceof Activity) {
						if (((ActivityMetric)metrics.getDetails()).getActivityBegin().getName().equals(((Activity)_activity).getName())) {			
							existe = true;
							break;
						}
						if (((ActivityMetric)metrics.getDetails()).getActivityEnd().getName().equals(((Activity)_activity).getName())) {						
							existe = true;
							break;
						} else existe = false;
					//}
				}
			if (!existe) error(" Invalid activity reference. \n Activity exists in different process related", MetricDslPackage.Literals.METRICS__DETAILS);
		}	
		
		/*
		 else if (metrics.getDetails() instanceof ActivityMetric) {
			//if (metrics.getRelatesTo().getLifecyleElement() instanceof Activity) {
				//System.out.print("ENTROU");
				for (EObject _activity : metrics.getRelatesTo().getLifecyleElement()) {	
					if (((ActivityMetric)metrics.getRelatesTo()).getActivityBegin().getName().equals(((Activity)_activity).getName())) {			
						existe = true;
						break;
					}
					if (((ActivityMetric)metrics.getRelatesTo()).getActivityEnd().getName().equals(((Activity)_activity).getName())) {						
						existe = true;
						break;
					}
				}
			//} //else if...
			if (!existe) error("Invalid activity reference", MetricDslPackage.Literals.METRICS__DETAILS);
		}
		 */
		else if (metrics.getDetails() instanceof TaskMetric) {		
			for (Task _task : metrics.getRelatesTo().getTasks() ) {	
				for (Task task : ((TaskMetric)metrics.getDetails()).getTasks())
					//System.out.print("Process-TASK: "+ _task.getName() + " Metric-TASK: "+ task.getName() + " v/f: " + task.getName().equals(_task.getName()) + "\n");
					if (task.getName().equals(_task.getName())) {
						existe = true;
						break;
					}
			}
			if (!existe) error("Invalid task reference. Task exists in different process related", MetricDslPackage.Literals.METRICS__DETAILS);
		}
		//if (!existe) 
			//error("Invalid task reference", MetricDslPackage.Literals.METRICS__DETAILS);
		
		//System.out.print("NOME DO ARTEFACT do processo: "+ _artifact.getName() + " \n");
		//System.out.print("NOME DO ARTEFACT: "+ artifact + " \n");
	}
	
}
